const app = getApp();
import {
  getSwiper,
} from '../../api/user';
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    swiperList: [],
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // reqeust swipers
    async getSwiperList() {
      const res = await getSwiper({
        sys_type: '2',
        bg_type: '2',
      });
console.log(res,'001');
      getApp().globalData.isInNationalTest = res.October_Is_Test == '1';

      if (!res.list) return console.warn('轮播图列表为空');

      this.setData({
        swiperList: res.list.map(item => ({
          ...item,
          bg_link: item.bg_link || item.cover_path,
          redirect: item.redirect || item.target_url,
        }))
      });
      console.log('swiperList', this.data.swiperList);
    },

    toBannerLink(e) {
      const { bannerlink: bannerUrl, type } = e.currentTarget.dataset;
      switch (type) {
        case 'toMiniProgram':
          wx.navigateToMiniProgram({
            appId: e.currentTarget.dataset.appid,
          });
          return;
        default:
          break;
      }

      if (!bannerUrl) return console.warn('bannerUrl为空，无法跳转');

      if (bannerUrl?.substr(0, 5) == '/page') {
        wx.switchTab({
          url: bannerUrl,
        });

        return;
      }
      // 如果以“subpkg”开头，则跳转对应页面
      if (bannerUrl?.startsWith('subpkg')) {
        wx.navigateTo({
          url: `/${bannerUrl}`
        });
        return;
      }
      wx.navigateTo({
        url: '/pages/index/bannerLink/bannerLink?banner=' + bannerUrl,
      });
    },
  },
  pageLifetimes: {
    show() {
      this.getSwiperList();
    }
  }
});
